package com.lzm.ydpt.module.order.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.byt.framlib.commonutils.DisplayUtil;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.lzm.ydpt.R;

/**
 * 取消订单原因对话框
 */
public class CancelCauseDialog implements View.OnClickListener {

    private Dialog mDialog;
    private View dialogView;

    private TextView tv_confirm_title;

    private LinearLayout ll_wrong_filling;
    private ImageView img_wrong_filling;
    private LinearLayout ll_no_more;
    private ImageView img_no_more;

    private EditText ed_cancel_cause;

    private TextView tv_close_dialog;
    private TextView tv_sub_cause;

    private Builder mBuilder;

    public CancelCauseDialog(Builder builder) {
        this.mBuilder = builder;
        mDialog = new Dialog(mBuilder.getContext(), R.style.MyDialogStyle);
        dialogView = View.inflate(mBuilder.getContext(), R.layout.dialog_cancel_cause, null);

        mDialog.setContentView(dialogView); // 一定要在setAttributes(lp)之前才有效
        //设置dialog的宽
        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = DisplayUtil.getScreenWidth(mBuilder.getContext());
        lp.height = DisplayUtil.getScreenHeight(mBuilder.getContext());
        dialogWindow.setAttributes(lp);
        mDialog.setCanceledOnTouchOutside(builder.isTouchOutside());

        tv_confirm_title = dialogView.findViewById(R.id.tv_confirm_title);

        ll_wrong_filling = dialogView.findViewById(R.id.ll_wrong_filling);
        img_wrong_filling = dialogView.findViewById(R.id.img_wrong_filling);
        ll_no_more = dialogView.findViewById(R.id.ll_no_more);
        img_no_more = dialogView.findViewById(R.id.img_no_more);

        ed_cancel_cause = dialogView.findViewById(R.id.ed_cancel_cause);

        tv_close_dialog = dialogView.findViewById(R.id.tv_close_dialog);
        tv_sub_cause = dialogView.findViewById(R.id.tv_sub_cause);

        if (mBuilder.getType() == 1) {
            tv_confirm_title.setText("更换手机号");
        } else if (mBuilder.getType() == 2) {
            tv_confirm_title.setText("更换登录密码");
        }
        img_wrong_filling.setSelected(true);
        img_no_more.setSelected(false);
        ed_cancel_cause.setHint("请填写其他原因");

        ll_wrong_filling.setOnClickListener(this);
        ll_no_more.setOnClickListener(this);

        tv_close_dialog.setOnClickListener(this);
        tv_sub_cause.setOnClickListener(this);
    }

    public void show() {
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
    }

    public void dismiss() {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        if (AntiShake.check(v.getId())) {
            return;
        }
        if (mBuilder == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.ll_wrong_filling:
                img_wrong_filling.setSelected(true);
                img_no_more.setSelected(false);
                break;
            case R.id.ll_no_more:
                img_wrong_filling.setSelected(false);
                img_no_more.setSelected(true);
                break;
            case R.id.tv_close_dialog:
                dismiss();
                if (mBuilder.getOnListener() != null) {
                    mBuilder.getOnListener().onClose();
                }
                break;
            case R.id.tv_sub_cause:
                dismiss();
                if (mBuilder.getOnListener() != null) {
                    mBuilder.getOnListener().onSubmit();
                }
                break;
        }
    }

    public static class Builder {

        private OnCancelCauseListener onListener;
        private boolean isTouchOutside;
        private Activity mContext;
        private String cause = "";
        private int type;

        public Builder(Activity context) {
            this.mContext = context;
            this.onListener = null;
            this.isTouchOutside = true;
            this.type = 1;
        }

        public OnCancelCauseListener getOnListener() {
            return onListener;
        }

        public Builder setOnListener(OnCancelCauseListener onListener) {
            this.onListener = onListener;
            return this;
        }

        public boolean isTouchOutside() {
            return isTouchOutside;
        }

        public Builder setCanceledOnTouchOutside(boolean isTouchOutside) {
            this.isTouchOutside = isTouchOutside;
            return this;
        }

        public CancelCauseDialog build() {
            return new CancelCauseDialog(this);
        }

        public Activity getContext() {
            return mContext;
        }

        public void setContext(Activity context) {
            mContext = context;
        }

        public void setTouchOutside(boolean touchOutside) {
            isTouchOutside = touchOutside;
        }

        public String getCause() {
            return cause;
        }

        public Builder setCause(String cause) {
            this.cause = cause;
            return this;
        }

        /**
         * 1 填错了
         * 2 不送了
         *
         * @return
         */
        public int getType() {
            return type;
        }

        public Builder setType(int type) {
            this.type = type;
            return this;
        }
    }

    public interface OnCancelCauseListener {
        /**
         * 提交
         */
        void onSubmit();

        /**
         * 关闭
         */
        void onClose();
    }
}